package com.android.kotlincoroutinemvvmdemo
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class DemoService : Service(), CoroutineScope by MainScope() {

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "class DemoService")

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationMg = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationMg.createNotificationChannel(
                NotificationChannel(
                    "80",
                    "App Service",
                    NotificationManager.IMPORTANCE_DEFAULT
                )
            )
            val builder = NotificationCompat.Builder(this,"80")
            startForeground(0xff, builder.build())
        }

        launch {
            while (true) {
                sendBroadcast(Intent().apply {
                    `package` = "com.android.kotlincoroutinemvvmdemo"
                    action = Constants.ACTION_IPC
                })
                delay(10 * 1000)
            }
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        return super.onStartCommand(intent, flags, startId)
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    companion object {
        const val TAG = "DemoService"
    }
}